import { ajax } from '../libs/ajax.js';
import { tools } from '../libs/tools.js';

//#region 获取数据

let list = [];
let page = {
  pageNumber: 1,
  pageSize: 5,
};

let queryInfo = { orderBy: '1', tname: '', league: '' };

function query() {
  queryInfo.orderBy = sel_order.value;
  queryInfo.tname = txt_tname.value;
  queryInfo.league = txt_league.value;
  ajax.get('/crud/footballteam', tools.mergeJson(page, queryInfo), (data) => {
    if (!data.success) {
      alert(data.message);
      return;
    }
    list = data.data.data;
    page = data.data.pageBean;
    showData();
    showPage();
  });
}

//#endregion

//#region tbody

let tbData = document.getElementById('tbData');
function showData() {
  tbData.innerHTML = '';
  list.forEach((item) => {
    let tr = document.createElement('tr');
    let td;
    td = document.createElement('td');
    // 编号
    td = document.createElement('td');
    td.append(item.ftid);
    tr.append(td);
    // 球队名称
    td = document.createElement('td');
    let a = document.createElement('a');
    a.setAttribute('href', `fbt_info.html?ftid=${item.ftid}`);
    a.append(item.tname);
    td.append(a);
    tr.append(td);
    // 所属联赛
    td = document.createElement('td');
    td.append(item.league);
    tr.append(td);
    // 联赛冠军数量
    td = document.createElement('td');
    td.append(item.champions);
    tr.append(td);
    // 杯赛冠军数量
    td = document.createElement('td');
    td.append(item.cups);
    tr.append(td);
    // 粉丝数(万)
    td = document.createElement('td');
    td.append(item.fans);
    tr.append(td);
    // 进球数记录
    td = document.createElement('td');
    td.append(item.goals);
    tr.append(td);
    // 进球数记录保持者
    td = document.createElement('td');
    td.append(item.gname);
    tr.append(td);
    // 俱乐部创建时间
    td = document.createElement('td');
    td.append(tools.formatDate(item.created, 'yyyy-MM-dd'));
    tr.append(td);

    tr.style.fontSize = '0.8rem';

    // 操作按钮
    // 修改
    td = document.createElement('td');
    let btnModi = document.createElement('button');
    btnModi.setAttribute('class', 'btn btn-success btn-sm');
    btnModi.style.marginRight = '0.5rem';
    btnModi.append('修改');
    btnModi.addEventListener('click', () => {
      location.href = 'fbt_modi.html?ftid=' + item.ftid;
    });
    td.append(btnModi);

    // 删除
    let btnDel = document.createElement('button');
    btnDel.setAttribute('class', 'btn btn-danger btn-sm');
    btnDel.append('删除');
    btnDel.addEventListener('click', () => {
      del(item);
    });
    td.append(btnDel);

    tr.append(td);

    tbData.append(tr);
  });
}

function del(intro) {
  console.log(intro);
  if (!confirm(`是否删除:${intro.tname}?`)) {
    return;
  }
  ajax.delete(`/crud/footballteam/${intro.ftid}`, {}, (data) => {
    alert(data.message);
    query();
  });
}
//#endregion

//#region 上部分
let txt_tname = document.getElementById('txt_tname');
let txt_league = document.getElementById('txt_league');
let sel_order = document.getElementById('sel_order');
let btnQuery = document.getElementById('btnQuery');
let btnReset = document.getElementById('btnReset');
let btnAdd = document.getElementById('btnAdd');

// 查询
btnQuery.addEventListener('click', () => {
  page.pageNumber = 1;
  query();
});

// 重置
btnReset.addEventListener('click', () => {
  txt_tname.value = '';
  txt_league.value = '';
  sel_order.value = 1;
  page.pageNumber = 1;
  query();
});

// 添加
btnAdd.addEventListener('click', () => {
  location.href = 'fbt_add.html';
});

//#endregion

//#region 下部分
let btnPre = document.getElementById('btnPre');
let spPage = document.getElementById('spPage');
let btnNext = document.getElementById('btnNext');

function showPage() {
  spPage.innerHTML = `第${page.pageNumber}页/共${page.pageCount}页`;
}

btnNext.addEventListener('click',()=>{
  page.pageNumber++;
  if (page.pageNumber > page.pageCount) {
    page.pageNumber = page.pageCount;
    return;
  }
  query();
})

btnPre.addEventListener('click', () => {
  page.pageNumber--;
  // 不可以小于1
  if (page.pageNumber < 1) {
    page.pageNumber = 1;
    return;
  }
  query();
});


//#endregion


query();
